<?php


include_once "libs/Smarty.class.php";
include_once "MyPDO.php";


class Student
{

    private $pagenum = 5;//每页显示5条记录

    //学生的列表
    //访问方式: index.php?action=student&func=lists&page=1
    function lists(){
        $smarty = new Smarty();
        $smarty->setCompileDir("smarty/compil");
        $smarty->setCacheDir("smarty/cache");
        $smarty->setConfigDir("smarty/config");
        $smarty->setTemplateDir("smarty/template");
        //获取页码 默认为1
        $page = isset($_GET["page"]) ? $_GET["page"] : 1;
        $smarty->assign("page",$page);
        //计算limit的值
        $cc = ($page - 1) * $this->pagenum;
        $ss=$page - 1;
        $smarty->assign("ss",$ss);
        $num1=0;
        $smarty->assign("num1",$num1);
        $i=1;
        $smarty->assign("i",$i);
        $ss1 = ($page + $i);
        $smarty->assign("ss1",$ss1);
        $db = MyPDO::getInstance();
        $sql = "select * from t_student ORDER BY create_time DESC  limit $cc,$this->pagenum ";
        //分页查询到学生的记录
        $students = $db->select($sql);
        $smarty->assign("students",$students);
        $sql2 = "select count(*) from T_student";
        $count_page = $db->select($sql2);
        //总页数 = 总记录数 / 每页数量
        $count_page = ceil($count_page[0]['count(*)'] / $this->pagenum);
        $smarty->assign("count_page",$count_page);
        //include "test.php";
        $smarty->display("test.php");
    }

    //查询
    //访问方式: index.php?action=student&func=add
    function add(){
        $smarty = new Smarty();

        $stuid = $_POST['stuid'];
        $name = $_POST['name'];
        $sex = $_POST['sex'];
        $date = $_POST['date'];
        $mobile = $_POST['mobile'];

        $smarty->assign("Stud_id",$stuid);
        $smarty->assign("Stud_name",$name);
        $smarty->assign("Stud_sex",$sex);
        $smarty->assign("Birth_date",$date);
        $smarty->assign("Mobile",$mobile);

        $db = MyPDO::getInstance();
        $sql = "insert into T_student(`Stud_id`,`Stud_name`,`Stud_sex`,`Birth_date`,`Mobile`) VALUES ('$stuid','$name','$sex','$date','$mobile');";
        $res = $db->insert($sql);

        if($res > 0){
            //跳转页面
            echo "<meta http-equiv=\"refresh\" content=\"1; url=index.php?action=student&func=lists&page=1\">";
            print('添加成功,正在拼命的跳转页面...<br>1秒后自动跳转~~~');
        }else{
            echo "添加失败";
        }
    }

    //3、修改
    //访问方式: index.php?action=student&func=edit
    function edit(){

        $stuid = $_POST['stuid'];
        $name = $_POST['name'];
        $sex = $_POST['sex'];
        $date = $_POST['date'];
        $mobile = $_POST['mobile'];

        $db = MyPDO::getInstance();

        $sql = "update T_student set Stud_id='$stuid',Stud_name='$name',Stud_sex='$sex',`Birth_date`='$date' ,`Mobile`='$mobile' where Stud_id = '$stuid';";

        $res = $db->insert($sql);
        if($res > 0){
            //跳转页面
            echo "<meta http-equiv=\"refresh\" content=\"1; url=index.php?action=student&func=lists&page=1\">";
            print('修改成功,正在拼命的跳转页面...<br>1秒后自动跳转~~~');
        }else{
            echo "修改失败";
        }
    }


    //1、先查询
    function findStudent(){
        $id = $_GET['id'];

        $db = MyPDO::getInstance();
        $sql = "select * from T_student where Stud_id=$id";
        $students = $db->select($sql);
        //有数据
        if(count($students) > 0){
            $student = $students[0];
            //2、带数据显示到页面上
            include "editstu.php";
        }

    }

    //删除
    function del()
    {
        if(!isset($_GET['id']))
        {
            $id = die("Error: 缺少必要参数id");
        }
        else
        {
            $id = $_GET['id'];
        }
        $db = MyPDO::getInstance();
        $sql="DELETE FROM T_student WHERE Stud_id = $id";
        $res = $db->insert($sql);
        if($res > 0){
            //跳转页面
            echo "<meta http-equiv=\"refresh\" content=\"1; url=index.php?action=student&func=lists&page=1\">";
            print('删除成功,正在拼命的跳转页面...<br>1秒后自动跳转~~~');
        }else{
            echo "删除失败";
        }
    }

    function __call($name, $arguments)
    {
        die("Error:温馨提醒,不存在{$name}方法");
    }
}